package com.example.service;


import cn.hutool.core.util.ObjectUtil;
import com.example.common.Constants;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Patients;
import com.example.exception.CustomException;
import com.example.mapper.PatientsMapper;
import com.example.utils.BcryptUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 患者表业务处理
 **/
@Service
public class PatientsService {

    @Resource
    private PatientsMapper patientsMapper;

    /**
     * 新增
     */
    public void add(Patients patients) {
        Patients dbPatients = patientsMapper.selectByUsername(patients.getUsername());
        if (ObjectUtil.isNotNull(dbPatients)) {
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        if (patients.getPassword().isEmpty()) {
            patients.setPassword(BcryptUtil.encode(Constants.USER_DEFAULT_PASSWORD));
        }else{
            patients.setPassword(BcryptUtil.encode(patients.getPassword()));
        }
        patients.setRole(RoleEnum.PATIENT.name());
        patientsMapper.insert(patients);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        patientsMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            patientsMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Patients Patients) {
        patientsMapper.updateById(Patients);
    }

    /**
     * 根据ID查询
     */
    public Patients selectById(Integer id) {
        return patientsMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Patients> selectAll(Patients Patients) {
        return patientsMapper.selectAll(Patients);
    }

    /**
     * 分页查询
     */
    public PageInfo<Patients> selectPage(Patients Patients, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Patients> list = patientsMapper.selectAll(Patients);
        return PageInfo.of(list);
    }

    /**
     * 注册
     */

    public void register(Account account) {
        Patients patient = new Patients();
        BeanUtils.copyProperties(account, patient);//拷贝属性
        patient.setName(account.getUsername());//患者姓名
        patient.setPassword(BcryptUtil.encode(account.getPassword()));
        patientsMapper.insert(patient);
    }

    /**
     * 登录（已弃用）
     */
    /*public Account login(Account account) {
        Account dbPatients = patientsMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbPatients)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST);
        }
        if (!account.getPassword().equals(dbPatients.getPassword())) {
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        return dbPatients;
    }*/



}